/*
 * Copyright (C) 2012 attmore
 * e-mail: attmore@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pocketsweet.ui.uilib;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.pocketsweet.R;
import com.pocketsweet.ui.uilib.adapter.GalleryAdapter;

/**
 * @author attmore
 */
public class GalleryView extends LinearLayout implements OnItemClickListener,
		OnItemLongClickListener {
	public static final int MAX_IMAGE_NUM = 8;
	private Context _ctx;
	private GridView mGrid;
	private GalleryAdapter adapter;
	private ArrayList<String> mUploadFilePaths;
	private IImagePickerListener listener;

	public GalleryView(Context context, AttributeSet attrs) {
		super(context, attrs);
		_ctx = context;
		initView();
	}

	public GalleryView(Context context) {
		super(context);
		_ctx = context;
		initView();
	}

	private void initView() {
		View root = inflate(_ctx, R.layout.view_grid_ex, null);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		addView(root, params);
		mGrid = (GridView) root.findViewById(R.id.grid);
		mUploadFilePaths = new ArrayList<String>();
		adapter = new GalleryAdapter(_ctx, mUploadFilePaths);
		mGrid.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mGrid.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		mGrid.setOnItemClickListener(this);
		mGrid.setOnItemLongClickListener(this);
	}
	
	

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (listener == null) {
			return;
		}
		if (mUploadFilePaths.size() >= MAX_IMAGE_NUM) {
			listener.onImagePickerItemClick(position, view);
		} else {
			if (position == mUploadFilePaths.size()) {
				listener.onImagePickerAddButtonClick();
			} else {
				if(position>mUploadFilePaths.size()){
					return;
				}
				listener.onImagePickerItemClick(position, view);
			}
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		if (listener == null) {
			return false;
		}
		if (mUploadFilePaths.size() >= MAX_IMAGE_NUM) {
			listener.onImagePickerItemLongClick(position);
		} else if (position != mUploadFilePaths.size()) {
			if(position>mUploadFilePaths.size()){
				return false;
			}
			listener.onImagePickerItemLongClick(position);
			return true;
		} else {
			// do nothing
		}
		return true;
	}

	public void addImagePath(String sPath) {
		if (!mUploadFilePaths.contains(sPath)) {
			mUploadFilePaths.add(sPath);
			adapter.notifyDataSetChanged();
		}
	}

	public ImageView getItem(int position) {
		return (ImageView) mGrid.getItemAtPosition(position);
	}

	/**
	 * ���յ�ַ�Ƴ�ͼƬ
	 * 
	 * @param sPath
	 */
	public void removeImagePath(String sPath) {

	}

	public void removeImagePath(int position) {
		if (position < 0 || position > MAX_IMAGE_NUM) {
			throw new IllegalArgumentException("wrong position");
		}
		mUploadFilePaths.remove(position);
		adapter.notifyDataSetChanged();
	}

	public ArrayList<String> getImagePaths() {
		return mUploadFilePaths;
	}

	public void setImagePickerListener(IImagePickerListener l) {
		this.listener = l;
	}

	public static interface IImagePickerListener {
		public void onImagePickerAddButtonClick();

		public void onImagePickerItemClick(int position, View view);

		public void onImagePickerItemLongClick(int position);
	}

	public void notifyDataSetChanged() {
		adapter.notifyDataSetChanged();
	}
}
